<template>
  <div>
    <el-row>
      <el-dialog v-dialogDrag :visible.sync="visible" :title="title" center :width="width">
        <div class="content">
          <el-steps :active="list.length"  align-center class="steps" v-if="list.length>0">
<!--            <el-step title="发起" disabled>-->
<!--            </el-step>-->
            <el-step
              v-for="item in list"
              :title="item.userUuid"
              :description="item.remarks"
              @click.native="handleClick(item)"
            >
              <div slot="title">
                <el-tag>
                  {{item.userUuid}}审核
                </el-tag>
              </div>
            </el-step>
<!--            <el-step title="结束" disabled>-->
<!--            </el-step>-->
          </el-steps>
          <div v-else style="line-height: 30px;margin-right: 10px">暂无数据，点击按钮添加</div>
          <div class="btn">
            <el-button type="primary" size="mini" icon="el-icon-plus" circle
                       @click="addNew"></el-button>
          </div>
        </div>

        <span class="dialog-footer" slot="footer" style="text-align: right!important;">
          <el-button size="mini" @click="visible = false">取 消</el-button>
<!--          <el-button size="mini" @click="save" type="primary">保 存</el-button>-->
        </span>
      </el-dialog>
    </el-row>
    <el-dialog v-dialogDrag
               v-if="dialog.visible"
               :visible.sync="dialog.visible"
               :title="dialog.title"
               center width="400px" append-to-body>
      <el-form
        :model="form"
        size="mini"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="审核人员" prop="userUuid">
          <el-select v-model="form.userUuid" :placeholder="rules.userUuid[0].message">
            <el-option v-for="item in userList" :key="item.uuid" :label="item.userName" :value="item.uuid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" :placeholder="rules.remarks[0].message">
          </el-input>
        </el-form-item>

      </el-form>
      <span class="dialog-footer" slot="footer" style="text-align: right!important;">
          <el-button size="mini" @click="dialog.visible = false">取 消</el-button>
          <el-button size="mini" type="danger" @click="removeOne" v-if="form.uuid">删 除</el-button>
          <el-button size="mini" @click="saveUser" type="primary">保 存</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
  import {APPROVALSTEP, USER} from "@/api"

  export default {
    components: {},
    props: {
      show: {Boolean, default: false},
      value: {Object, default: {}},
      width: {String, default: "30%"},
      title: {String, default: "title"},
      type: {Number, default: 0} // 0 新增 1 编辑 2 查看
    },
    data() {
      return {
        editType: 0,
        form: {},
        list: [],
        dialog: {
          visible: false,
          title: '人员选择'
        },
        userList: [],
        rules: {
          userUuid: [{required: true, message: "请选择审核人", trigger: "blur"}],
          remarks: [{required: false, message: "请输入备注", trigger: "blur"}]
        }
      }
    },
    computed: {
      visible: {
        get() {
          return this.show
        },
        set(val) {
          this.$emit("update:show", false)
        }
      }
    },
    mounted() {
      this.setOldData(this.value)
      this.getUserList()
      this.getSteps()
    },
    watch: {},
    methods: {
      setOldData(data) {
        this.form = this.$newObject(data)
      },
      getSteps() {
        const params = {
          procedureId: this.value.uuid
        }
        APPROVALSTEP.list(params).then(res => {
          this.list = res.data
        })

      },
      getUserList() {
        USER.list({}).then(res => {
          if (res.code === 200) {
            this.userList = res.data
          } else {
            this.userList = []
          }
        })
      },
      handleClick(row) {
        this.form = row
        this.dialog.visible = true
        this.editType = 1
      },
      saveUser() {
        if (this.editType === 0) {
          let orderIndex = this.list.length
          const data = {
            procedureId: this.value.uuid,
            orderIndex,
            userUuid: this.form.userUuid,
            remarks: this.form.remarks
          }
          APPROVALSTEP.save(data).then(res => {
            if (res.code === 200) {
              this.$message.success(res.message)
              this.getSteps()
            } else {
              this.$message.error(res.message)
            }
          })
          this.dialog.visible = false
        } else {
          APPROVALSTEP.update(this.form).then(res => {
            if (res.code === 200) {
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
          })
          this.dialog.visible = false
        }
      },
      removeOne() {
        APPROVALSTEP.remove([this.form.uuid]).then(res => {
          if (res.code === 200) {
            const params = {
              procedureId: this.value.uuid
            }
            APPROVALSTEP.list(params).then(res => {
              if (res.code === 200) {
                this.list = res.data
                if(this.list.length ===0) {
                  this.dialog.visible = false
                  return
                }
                const list = this.list.map((l,i) => {
                  l.orderIndex = i
                  return l
                })
                APPROVALSTEP.updateList(list).then(res => {
                  if (res.code === 200) {
                    this.$message.success('更新成功')
                    this.getSteps()
                  } else {
                    this.$message.error(res.message)
                  }

                })
              }
              this.dialog.visible = false
            })

          }
        })
      },
      addNew() {
        this.dialog.visible = true
        this.editType = 0
        this.form = {
          procedureId: this.value.uuid
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .small-card {
    ::v-deep .el-card__body {
      padding: 10px;
    }
  }

  .el-form {
    ::v-deep .el-form-item {
      margin-bottom: 10px;
    }

  }

  ::v-deep .el-step__icon {
    color: red;

    &:hover {
      cursor: pointer;
    }
  }

  .content {
    display: flex;

    .steps {
      flex: 1;

      .btn {
        width: 80px;
      }
    }
  }
</style>
